import { t } from 'i18next'
import { useCreateContentStore } from '../../store'
import { useMeetingParamsStore } from './store'
import { useMeetingMixStore } from '../mix/store'
import { useMeetingPollStore } from '../poll/store'
import Alert from '@/components/Alert'
import { useBasicInfoStore } from '../basicInfo/store'
import { resetProtocolNotRtc } from '../user/store.utils'
import { useGlobalStore } from '@/store/global'
import { checkPermission } from '@/utils'
import { checkSelfMediaValueInList, resetBitrateType, setSelfMediaAbilityList } from './store.media'

export const initMeetingTypeList = () => {
	let meetingTypeList = []
	let defaultMeetingType = 0
	const permission = useCreateContentStore.getState().permission
	const SWITCH = useGlobalStore.getState().SWITCH

	if (permission.enableTrans && checkPermission('100', SWITCH.create.meetingType)) {
		defaultMeetingType = 0 //默认传统
		meetingTypeList.push({ label: t('传统会议'), value: 0 })
	}
	if (permission.enablePortMedia && checkPermission('010', SWITCH.create.meetingType)) {
		defaultMeetingType = 1 //默认端口
		meetingTypeList.push({ label: t('端口会议'), value: 1 })
	}

	if (permission.enableSfu && checkPermission('001', SWITCH.create.meetingType)) {
		defaultMeetingType = defaultMeetingType === 1 ? 1 : 2 //默认sfu
		meetingTypeList.push({ label: t('超元RTC会议'), value: 2 })
	}
	meetingTypeList = meetingTypeList.length === 0 ? [{ label: t('传统会议'), value: 0 }] : meetingTypeList
	useMeetingParamsStore.setState({ _meetingTypeList: meetingTypeList })
	return defaultMeetingType as 0 | 1 | 2
}

/** 切换时协议恢复默认值 */
export function resetMeetingProtocol() {
	const { enableRtc, _H265, encryptionType } = useMeetingParamsStore.getState()
	useMeetingParamsStore.setState({
		meetingProtocol: enableRtc && !_H265 && encryptionType !== 3 ? 2 : 1,
	})
}

export function changeVoiceInspireEnable(v: boolean, needAlert?: boolean) {
	if (v) {
		//语音激励和定制混音互斥
		if (useMeetingMixStore.getState().mode === 2 && useMeetingMixStore.getState().enable) {
			needAlert && Alert(t('语音激励和定制混音不能同时开启，请先停止定制混音'))
			return false
		}

		//语音激励和轮询成为发言方互斥
		if (useMeetingPollStore.getState().enablePoll && useMeetingPollStore.getState().isSpeaker) {
			needAlert && Alert(t('您已设置轮询到的终端成为发言人，无需重复设置'))
			return false
		}
		//
		if (useBasicInfoStore.getState().speaker.uid !== '') {
			needAlert && Alert(t('您已设置会场发言人，无需重复设置'))
			return false
		}
		useMeetingParamsStore.setState({ voiceInspireEnable: v, voiceInspireTime: 3 })
	} else {
		useMeetingParamsStore.setState({ voiceInspireEnable: v, voiceInspireTime: 3 })
	}
}

export function changeEncryptionType(val: 0 | 2 | 3 | 4) {
	useMeetingParamsStore.setState({
		encryptionType: val,
		encryptedSM: false,
		encryptedAuth: false,
		encryptedKey: '',
		quantumEncryption: false,
	})
	resetMeetingProtocol()
}

export function changeRtc(val: boolean) {
	const { meetingType, rate, _bitrateType } = useMeetingParamsStore.getState()
	const permission = useCreateContentStore.getState().permission
	if (val) {
		useMeetingParamsStore.setState({
			enableRtc: val,
			sourceUsed: permission.integrated ? 1 : 0,
		})
		changeH265(false)
		changeVoiceInspireEnable(true)
	} else {
		useMeetingParamsStore.setState({
			enableRtc: val,
			sourceUsed: permission.integrated ? 1 : 0,
		})
		changeVoiceInspireEnable(false)
		resetProtocolNotRtc()
	}
	if (_bitrateType === 0) {
		setSelfMediaAbilityList(rate) //修改下拉列表数据
		checkSelfMediaValueInList()
	}

	if (meetingType === 2) {
		changeEncryptionType(2)
	} else {
		changeEncryptionType(0)
	}
}

export function changeMeetingType(val: 0 | 1 | 2) {
	const permission = useCreateContentStore.getState().permission

	useMeetingParamsStore.setState({
		meetingType: val,
	})
	resetBitrateType()
	if ((val === 0 && permission.tradMix === 2) || (val === 1 && permission.portMix === 2) || val === 2) {
		changeRtc(true)
	} else {
		changeRtc(false)
	}

	if (val === 2) {
		useMeetingParamsStore.setState({
			voiceInspireEnable: false,
		})
	}
}

export function changeH265(val: boolean) {
	const { _bitrateType, _H265, rate } = useMeetingParamsStore.getState()
	if (val === _H265) return
	useMeetingParamsStore.setState({
		_H265: val,
		video: val ? 6 : 4,
	})
	if (_bitrateType !== 0) {
		resetBitrateType()
	} else {
		setSelfMediaAbilityList(rate) //修改下拉列表数据
		checkSelfMediaValueInList()
	}
}
